const app = getApp()
import {
  getProdActIdList
} from "/api/product"

function activeProMoney(data) {
  // console.log("data=>>{}",data)
  return new Promise(async (resolve, reject) => {
    // let arr = app.globalData.productId || await _getProductIdList();
    let arr = app.globalData.productId || [];
    // console.log("arr",arr)
    let id = data.params.id
    if (typeof id == 'string') {
      id = parseInt(id)
    }
    // console.log("计算价格",arr,id,arr.includes(id))
    if (arr.includes(id)) {
      let yu = data.params.model.price % data.leaseTerm;
      let meiqiPrice = (data.params.model.price - yu) / data.leaseTerm
      data.lastRent = (meiqiPrice / 100).toFixed(2)
      data.firstRent = 1
      data.allRent = ((data.params.model.price / 100) - data.lastRent + data.firstRent).toFixed(2)
    } else {
      data.allRent = (data.params.model.price / 100).toFixed(2)
    }
    resolve(data)
  })
}
// 获取商品一元租id
function _getProductIdList() {
  return new Promise((resolve, reject) => {
    getProdActIdList().then((res => {
      console.log(res)
      if (res.code == 200) {
        app.globalData.productId = res.data
        resolve(res.data)
      } else reject([])
    }))
  })
}

module.exports = {
  activeProMoney,
  _getProductIdList
}